package tree.medium;

import tree.TreeNode;

/**
 * 验证二叉搜索树
 * 输入：root = [2,1,3]
 * 输出：true
 *
 * 输入：root = [5,1,4,null,null,3,6]
 * 输出：false
 * 解释：根节点的值是 5 ，但是右子节点的值是 4 。
 */
public class ValidateBinarySearchTree_98 {
    public boolean isValidBST(TreeNode root) {
        return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean dfs(TreeNode node, long left, long right) {
        if (node == null)
            return true;
        if (node.val <= left)
            return false;
        if (node.val >= right)
            return false;

        return dfs(node.left, left, node.val) && dfs(node.right, node.val, right);
    }

}
